package tech.aistar.corejava.day12.homework.home03;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/7/30
 * @Version: 1.0
 */
public class ProductDaoImlp implements IProductDao{
    List<Product> products = new ArrayList<>();

    public ProductDaoImlp(){

    }

    @Override
    public List<Product> findAll(){
        return products;
    }

    @Override
    public void add(Product p) {
        products.add(p);
    }

    @Override
    public void select(Integer id) {
        if(products.isEmpty()){
            return;
        }
        System.out.println(products.get(id - 1));

    }

    @Override
    public void delete(Integer id) {
        if(products.isEmpty()){
            return;
        }
        Iterator<Product> iter = products.iterator();
        while(iter.hasNext()){
            Product p = iter.next();
            if(p.getId().equals(id)){
                iter.remove();
            }
        }

    }

    @Override
    public void update(Integer id, double times) {
        if(products.isEmpty()){
            return;
        }
        products.get(id - 1).setPrice(products.get(id - 1).getPrice()*times);
    }
}
